package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.common.CustomException;
import com.itheima.health.entity.CheckGroup;
import com.itheima.health.entity.CheckItem;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public void deleteById(Integer id) {
        //条件构造器
        LambdaQueryWrapper<CheckItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckItem::getId,id);

        int count = checkItemService.count(queryWrapper);
        //当检查组中关联检查项
        if(count > 0){
            throw new CustomException("请先删除检查项");
        }
        super.removeById(id);
    }
}


